/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.irssproject.app.entitats;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

/**
 *
 * @author adria
 */
@Entity
@Table(name = "items")
@NamedQueries({
    @NamedQuery(name = "Item.findAll", query = "SELECT i FROM Item i")})
public class Item implements Serializable, Comparable<Object> {

    private static final long serialVersionUID = 1L;
    private Long id;
    private String link;
    private String title;
    private String content;
    private Date publishedDate;
    private Feed feed;

    public Item() {
    }

    public Item(Long iditem) {
        this.id = iditem;
    }

    public Item(Long iditem, String link, String titol, Date dataPublicacio) {
        this.id = iditem;
        this.link = link;
        this.title = titol;
        this.publishedDate = dataPublicacio;
    }

    public Item(String itemURL) {
        this.link = itemURL;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "iditem")
    public Long getId() {
        return id;
    }

    public void setId(Long iditem) {
        this.id = iditem;
    }

    @Basic(optional = false)
    @Column(name = "link")
    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    @Basic(optional = false)
    @Column(name = "titol")
    public String getTitle() {
        return title;
    }

    public void setTitle(String titol) {
        this.title = titol;
    }

    @Lob
    @Column(name = "contingut")
    public String getContent() {
        return content;
    }

    public void setContent(String contingut) {
        this.content = contingut;
    }

    @Basic(optional = false)
    @Column(name = "data_publicacio")
    @Temporal(value = TemporalType.TIMESTAMP)
    public Date getPublishedDate() {
        return publishedDate;
    }

    public void setPublishedDate(Date dataPublicacio) {
        this.publishedDate = dataPublicacio;
    }

    @JoinColumn(name = "idfeed", referencedColumnName = "idfeed")
    @ManyToOne(optional=false, fetch = FetchType.EAGER)
    public Feed getFeed() {
        return feed;
    }

    public void setFeed(Feed feed) {
        this.feed = feed;
    }

    @Transient
    public String getDataPublicacioString() {
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy 'at' HH:mm", Locale.getDefault());
        return sdf.format(this.publishedDate);

    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Item)) {
            return false;
        }
        Item other = (Item) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.irssproject.app.entitats.Item[iditem=" + id + "]";
    }
    
        @Override
    public int compareTo(Object o) {

        int compareResult = this.getPublishedDate().compareTo(((Item) o).getPublishedDate());

        if (compareResult == 0) {
            return 0;
        } else if (compareResult > 0) {
            return -1;
        } else {
            return 1;
        }
    }

}
